

package com.liulishuo.filedownloader.i;

import com.liulishuo.filedownloader.message.MessageSnapshot;
import com.liulishuo.filedownloader.model.FileDownloadHeader;
import com.liulishuo.filedownloader.model.FileDownloadTaskAtom;
import ohos.event.notification.NotificationRequest;
import com.liulishuo.filedownloader.i.IFileDownloadIPCCallback;
import com.liulishuo.filedownloader.i.FileDownloadIPCCallbackStub;
import com.liulishuo.filedownloader.i.FileDownloadIPCServiceProxy;
import ohos.rpc.IRemoteBroker;
import ohos.rpc.IRemoteObject;
import ohos.rpc.RemoteException;
import ohos.rpc.RemoteObject;
import ohos.rpc.MessageParcel;
import ohos.rpc.MessageOption;

public abstract class FileDownloadIPCServiceStub extends RemoteObject implements IFileDownloadIPCService {
    private static final String DESCRIPTOR = "com.liulishuo.filedownloader.i.IFileDownloadIPCService";

    private static final int COMMAND_REGISTER_CALLBACK = IRemoteObject.MIN_TRANSACTION_ID + 0;
    private static final int COMMAND_UNREGISTER_CALLBACK = IRemoteObject.MIN_TRANSACTION_ID + 1;
    private static final int COMMAND_CHECK_DOWNLOADING = IRemoteObject.MIN_TRANSACTION_ID + 2;
    private static final int COMMAND_START = IRemoteObject.MIN_TRANSACTION_ID + 3;
    private static final int COMMAND_PAUSE = IRemoteObject.MIN_TRANSACTION_ID + 4;
    private static final int COMMAND_PAUSE_ALL_TASKS = IRemoteObject.MIN_TRANSACTION_ID + 5;
    private static final int COMMAND_SET_MAX_NETWORK_THREAD_COUNT = IRemoteObject.MIN_TRANSACTION_ID + 6;
    private static final int COMMAND_GET_SOFAR = IRemoteObject.MIN_TRANSACTION_ID + 7;
    private static final int COMMAND_GET_TOTAL = IRemoteObject.MIN_TRANSACTION_ID + 8;
    private static final int COMMAND_GET_STATUS = IRemoteObject.MIN_TRANSACTION_ID + 9;
    private static final int COMMAND_IS_IDLE = IRemoteObject.MIN_TRANSACTION_ID + 10;
    private static final int COMMAND_START_FOREGROUND = IRemoteObject.MIN_TRANSACTION_ID + 11;
    private static final int COMMAND_STOP_FOREGROUND = IRemoteObject.MIN_TRANSACTION_ID + 12;
    private static final int COMMAND_CLEAR_TASK_DATA = IRemoteObject.MIN_TRANSACTION_ID + 13;
    private static final int COMMAND_CLEAR_ALL_TASK_DATA = IRemoteObject.MIN_TRANSACTION_ID + 14;

    private static final int ERR_OK = 0;
    private static final int ERR_RUNTIME_EXCEPTION = -1;

    public FileDownloadIPCServiceStub(
        /* [in] */ String descriptor) {
        super(descriptor);
    }

    @Override
    public IRemoteObject asObject() {
        return this;
    }

    public static IFileDownloadIPCService asInterface(IRemoteObject object) {
        if (object == null) {
            return null;
        }

        IFileDownloadIPCService result = null;
        IRemoteBroker broker = object.queryLocalInterface(DESCRIPTOR);
        if (broker != null) {
            if (broker instanceof IFileDownloadIPCService) {
                result = (IFileDownloadIPCService)broker;
            }
        } else {
            result = new FileDownloadIPCServiceProxy(object);
        }

        return result;
    }

    @Override
    public boolean onRemoteRequest(
        /* [in] */ int code,
        /* [in] */ MessageParcel data,
        /* [out] */ MessageParcel reply,
        /* [in] */ MessageOption option) throws RemoteException {
        String token = data.readInterfaceToken();
        if (!DESCRIPTOR.equals(token)) {
            return false;
        }
        switch (code) {
            case COMMAND_REGISTER_CALLBACK: {
                IFileDownloadIPCCallback callback = FileDownloadIPCCallbackStub.asInterface(data.readRemoteObject());
                registerCallback(callback);
                reply.writeNoException();
                return true;
            }
            case COMMAND_UNREGISTER_CALLBACK: {
                IFileDownloadIPCCallback callback = FileDownloadIPCCallbackStub.asInterface(data.readRemoteObject());
                unregisterCallback(callback);
                reply.writeNoException();
                return true;
            }
            case COMMAND_CHECK_DOWNLOADING: {
                String url = data.readString();
                String path = data.readString();
                boolean result;
                result = checkDownloading(url, path);
                reply.writeNoException();
                reply.writeInt(result ? 1 : 0);
                return true;
            }
            case COMMAND_START: {
                String url = data.readString();
                String path = data.readString();
                boolean pathAsDirectory = data.readInt() == 1 ? true : false;
                int callbackProgressTimes = data.readInt();
                int callbackProgressMinIntervalMillis = data.readInt();
                int autoRetryTimes = data.readInt();
                boolean forceReDownload = data.readInt() == 1 ? true : false;
                FileDownloadHeader header = new FileDownloadHeader();
                data.readSequenceable(header);
                boolean isWifiRequired = data.readInt() == 1 ? true : false;
                start(url, path, pathAsDirectory, callbackProgressTimes, callbackProgressMinIntervalMillis, autoRetryTimes, forceReDownload, header, isWifiRequired);
                reply.writeNoException();
                return true;
            }
            case COMMAND_PAUSE: {
                int downloadId = data.readInt();
                boolean result;
                result = pause(downloadId);
                reply.writeNoException();
                reply.writeInt(result ? 1 : 0);
                return true;
            }
            case COMMAND_PAUSE_ALL_TASKS: {
                pauseAllTasks();
                reply.writeNoException();
                return true;
            }
            case COMMAND_SET_MAX_NETWORK_THREAD_COUNT: {
                int count = data.readInt();
                boolean result;
                result = setMaxNetworkThreadCount(count);
                reply.writeNoException();
                reply.writeInt(result ? 1 : 0);
                return true;
            }
            case COMMAND_GET_SOFAR: {
                int downloadId = data.readInt();
                long result;
                result = getSofar(downloadId);
                reply.writeNoException();
                reply.writeLong(result);
                return true;
            }
            case COMMAND_GET_TOTAL: {
                int downloadId = data.readInt();
                long result;
                result = getTotal(downloadId);
                reply.writeNoException();
                reply.writeLong(result);
                return true;
            }
            case COMMAND_GET_STATUS: {
                int downloadId = data.readInt();
                byte result;
                result = getStatus(downloadId);
                reply.writeNoException();
                reply.writeInt(result);
                return true;
            }
            case COMMAND_IS_IDLE: {
                boolean result;
                result = isIdle();
                reply.writeNoException();
                reply.writeInt(result ? 1 : 0);
                return true;
            }
            case COMMAND_START_FOREGROUND: {
                int id = data.readInt();
                NotificationRequest notification = new NotificationRequest();
                data.readSequenceable(notification);
                startForeground(id, notification);
                reply.writeNoException();
                return true;
            }
            case COMMAND_STOP_FOREGROUND: {
                boolean removeNotification = data.readInt() == 1 ? true : false;
                stopForeground(removeNotification);
                reply.writeNoException();
                return true;
            }
            case COMMAND_CLEAR_TASK_DATA: {
                int id = data.readInt();
                boolean result;
                result = clearTaskData(id);
                reply.writeNoException();
                reply.writeInt(result ? 1 : 0);
                return true;
            }
            case COMMAND_CLEAR_ALL_TASK_DATA: {
                clearAllTaskData();
                reply.writeNoException();
                return true;
            }
            default:
                return super.onRemoteRequest(code, data, reply, option);
        }
    }
};

